package com.xinsoft.controller.device;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.util.RandomUtil;
import com.alibaba.fastjson.JSONArray;
import com.google.common.collect.Maps;
import com.xinsoft.common.Response;
import com.xinsoft.entity.dto.CusPointValueHisDTO;
import com.xinsoft.entity.dto.CusTestPointPageParam;
import com.xinsoft.entity.vo.CusPointDataVo;
import com.xinsoft.entity.vo.CusDevTestPointVo;
import com.xinsoft.service.CusTestPointService;
import com.xinsoft.service.InfluxService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.text.ParseException;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * @ClassName CusPointDataController
 * @Description 测点数据控制层
 * @Author lijie
 * @Date 2023-6-13 12:53:16
 * @Version 1.0
 */
@RestController
@RequestMapping("/cus/point/data")
@Api(value = "CusPointDataController", tags = {"测点数据控制层"})
public class CusPointDataController {

    @Autowired
    private CusTestPointService cusCusTestPointService;

    @Autowired
    private InfluxService influxService;

    /**
     * @return com.xinsoft.common.Response<?>
     * @Author Li9527
     * @Description 实时数据接口
     * @Date 2023/7/7 12:34
     * @params cusCusTestPointPageParam
     **/
    @PostMapping("/current")
    @ApiOperation("实时数据接口")
    public Response<?> current(@RequestBody CusTestPointPageParam cusCusTestPointPageParam) {
        List<CusDevTestPointVo> list = cusCusTestPointService.findListByParam(cusCusTestPointPageParam);
        List<CusPointDataVo> pointDataVoList = new ArrayList<>();
        for (int i = 0; i < list.size(); i++) {
            CusPointDataVo cusPointDataVo = new CusPointDataVo();
            BeanUtil.copyProperties(list.get(i), cusPointDataVo);
            cusPointDataVo.setPointValue(RandomUtil.randomNumbers(5));
            pointDataVoList.add(cusPointDataVo);
        }
        return Response.succeed(pointDataVoList);
    }

   /**
    * @Author Li9527
    * @Description 历史数据接口
    * @Date 2023/7/10 13:55
    * @params   cusPointValueHisDTO
    * @return com.xinsoft.common.Response
    **/
    @PostMapping("/history")
    @ApiOperation("历史数据接口")
    public Response list(@RequestBody CusPointValueHisDTO cusPointValueHisDTO) throws ParseException {
        Map<String, Object> results = Maps.newHashMap();
        JSONArray result = influxService.list(cusPointValueHisDTO);
        results.put("result", result);
        return Response.succeed(results);
    }

}
